package com.dtw.ditouwang.ui.more;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dtw.ditouwang.R;
import com.dtw.ditouwang.beans.CommentBean;
import com.dtw.ditouwang.models.WebApi;
import com.dtw.ditouwang.ui.OnItemClickListener;

import java.util.List;

public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.Holder> {
    private List<CommentBean.DataBean> commentList;
    private OnItemClickListener onItemClickListener;

    public CommentAdapter(List<CommentBean.DataBean> commentList) {

        this.commentList = commentList;
    }

    public void setMoreCommentListener(OnItemClickListener onItemClickListener) {

        this.onItemClickListener = onItemClickListener;
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new Holder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_comment, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull Holder holder, int i) {
        holder.name.setText(commentList.get(i).getNickname());
        holder.time.setText(commentList.get(i).getTimes());
        holder.awesome.setText(commentList.get(i).getZan() + "");
        holder.awesome.setSelected(commentList.get(i).getIs_click()==1);
        holder.content.setText(commentList.get(i).getInfo());
        if(commentList.get(i).getHead()!=null) {
            Glide.with(holder.head).load(WebApi.BaseUrl + commentList.get(i).getHead().replace("\\", "/")).into(holder.head);
        }else{
            holder.head.setImageBitmap(null);
        }
        if (commentList.get(i).getChildren().size() > 0) {
            holder.contentChildFirst.setText(Html.fromHtml("<font><b>" + commentList.get(i).getChildren().get(0).getC_nickname() +
                    "：</b></font>" + commentList.get(i).getChildren().get(0).getC_content()));
            holder.contentChildFirst.setVisibility(View.VISIBLE);
        }
        if (commentList.get(i).getChildren().size() > 1) {
            holder.contentChildSecond.setText(Html.fromHtml("<font><b>" + commentList.get(i).getChildren().get(1).getC_nickname() +
                    "：</b></font>" + commentList.get(i).getChildren().get(1).getC_content()));
            holder.contentChildSecond.setVisibility(View.VISIBLE);
        }
        if (commentList.get(i).getChildren().size() > 2) {
            holder.contentChildMore.setText("查看全部" + commentList.get(i).getChildren().size() + "条回复");
            holder.contentChildMore.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public int getItemCount() {
        return commentList.size();
    }

    class Holder extends RecyclerView.ViewHolder {
        TextView name, time, awesome, content, contentChildFirst, contentChildSecond, contentChildMore;
        ImageView head;

        public Holder(@NonNull View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.text_name);
            time = itemView.findViewById(R.id.text_time);
            awesome = itemView.findViewById(R.id.text_awesome);
            content = itemView.findViewById(R.id.text_content);
            contentChildFirst = itemView.findViewById(R.id.text_comment_child_first);
            contentChildSecond = itemView.findViewById(R.id.text_comment_child_second);
            contentChildMore = itemView.findViewById(R.id.text_comment_child_more);
            head = itemView.findViewById(R.id.img_head);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(onItemClickListener!=null){
                        onItemClickListener.onItemClick(R.id.text_comment_child_more,getAdapterPosition(),0);
                    }
                }
            });
            awesome.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(awesome.isSelected()){
                        awesome.setSelected(false);
                        awesome.setText(Integer.parseInt(awesome.getText().toString())-1+"");
                    }else{
                        awesome.setSelected(true);
                        awesome.setText(Integer.parseInt(awesome.getText().toString())+1+"");
                    }
                    if(onItemClickListener!=null){
                        onItemClickListener.onItemClick(R.id.text_awesome, getAdapterPosition(),awesome.isSelected()?1:0);
                    }
                }
            });
        }
    }
}
